/*
 * Copyright (c) 2017. All  rights reserved.
 * 项目名：microservice-base
 * 文件名：UserUtils.java
 * Date  ：17-11-30 下午2:37
 * Author：abin
 *
 */

package com.microservice.base.common.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserUtils {

    public static String getUserId() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        String user = String.valueOf(authentication.getPrincipal());
        String[] userStr = user.split(",");
        return userStr[0];
    }

    public static String getUseName() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        return String.valueOf(authentication.getCredentials());
    }

    public static String getSchoolCode() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        String user = String.valueOf(authentication.getPrincipal());
        String[] userStr = user.split(",");
        return userStr[1];
    }

    public static String realName() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            String user = String.valueOf(authentication.getPrincipal());
            String[] userStr = user.split(",");
            return userStr[2];
        } else {
            return null;
        }
    }
}
